package com.dingding.queue.queue.consumer.listener;

import java.io.UnsupportedEncodingException;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.dingding.common.core.beans.UserTaxiOrdert;
import com.dingding.common.core.util.JSONUtil;
import com.dingding.facade.special.ISpecialOrderFacade;

@Component("appointTaxiOrderListener")
public class AppointTaxiOrderListener implements MessageListener {

	@Resource(name = "specialOrderFacade")
	private ISpecialOrderFacade specialOrderFacade;

	@Override
	public Action consume(Message message, ConsumeContext context) {

		Logger log = Logger.getLogger(this.getClass());

		String request = "";
		try {
			request = new String(message.getBody(), "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			log.error("指派出租车订单过程中产生错误！", e1);
			return Action.ReconsumeLater;
		}
		
		UserTaxiOrdert taxiOrdert = JSONUtil.parseObject(request, UserTaxiOrdert.class);
		
		if(this.specialOrderFacade.appointTaxiOrder(taxiOrdert) == true){
			return Action.CommitMessage;
		}else{
			return Action.ReconsumeLater;
		}
	}

}
